package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.Dish;
import com.itheima.reggin.entity.Orders;
import com.itheima.reggin.entity.Report;
import com.itheima.reggin.entity.Setmeal;
import com.itheima.reggin.service.DishService;
import com.itheima.reggin.service.OrderDetailService;
import com.itheima.reggin.service.OrdersService;
import com.itheima.reggin.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private DishService dishService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 套餐图表
     * @return
     */

    @GetMapping("/setmeal")
    public R<List<Report>> reports() {
        //获取套餐所有数据
        List<Setmeal> setmeals = setmealService.list();
        //2.
        ArrayList<Report> setMealReports = new ArrayList<>();
        //3.
        for (Setmeal setmeal : setmeals) {
            Long setmealId = setmeal.getId();
            String name = setmeal.getName();
            Integer sum = orderDetailService.saleNums(setmealId);

            //2.创建套餐报表对象
            Report report = new Report();
            report.setName(name);
            report.setValue(sum);
            //添加到集合中
            setMealReports.add(report);
        }
        return R.success(setMealReports);

    }


    /**
     * 菜品图表
     * @return
     */
    @RequestMapping("/dish")
    public R report() {
        //1.用于获得已支付的所有订单号
        LambdaQueryWrapper<Orders> ordersLqw = new LambdaQueryWrapper<>();
        ordersLqw.lt(Orders::getStatus, 1);
        List<Orders> orders = ordersService.list(ordersLqw);

        //2.获得所有的菜品号
        List<Dish> dishes = dishService.list();
        Report report = new Report();
        //3.
        ArrayList<String> categories = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();
        //4.
        for (Dish dish : dishes) {
            //1.向数组中添加菜品
            categories.add(dish.getName());
            Integer integer = orderDetailService.saleNum(dish.getId());
            if (integer == null) {
                values.add("0");
            } else {
                values.add(String.valueOf(integer));

            }

        }
        report.setCategories(categories);
        report.setValues(values);
        return R.success(report);
    }

}